﻿using System.Windows;
using System.Windows.Controls;

namespace FFmpegVideoClip.Lib.Controls
{
    public class CheckButton : CheckBox
    {
        public CheckButton()
        {
            this.Checked += CheckButton_Checked;
            this.Unchecked += CheckButton_Checked;
        }

        public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(CheckButton), new PropertyMetadata(string.Empty));

        public bool IsButtonChecked { get => (bool)GetValue(IsButtonCheckedProperty); set => SetValue(IsButtonCheckedProperty, value); }
        public static readonly DependencyProperty IsButtonCheckedProperty = DependencyProperty.Register("IsButtonChecked", typeof(bool), typeof(CheckButton), new PropertyMetadata(false, ButtonCheckedChanged));
        private static void ButtonCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CheckButton button = (CheckButton)d;
            bool value = (bool)e.NewValue;
            if (button.IsChecked != value)
                button.IsChecked = value;
        }
        private void CheckButton_Checked(object sender, RoutedEventArgs e)
        {
            if (IsChecked == IsButtonChecked)
                return;
            IsButtonChecked = IsChecked.HasValue ? IsChecked.Value : false;
        }

        //protected override void OnToggle()
        //{
        //    base.OnToggle();
        //    IsButtonChecked = IsChecked.HasValue ? IsChecked.Value : false;
        //}
    }
}
